﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
namespace NCKH.Admin
{
    public partial class Hoidong : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                hienhoidong();
                // hienloaihinh();
            }
        }


        protected void hienhoidong()
        {
            using (DataTable HOIDONG = getHoidong())
            {
                grvhoidong.DataSource = HOIDONG;
                grvhoidong.DataBind();
            }
        }
        DataTable getHoidong()
        {

            string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            using (SqlConnection cnn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = new SqlCommand("Select_hoidong", cnn))
                {
                    using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                    {
                        DataTable tbl = new DataTable("HOIDONG");
                        da.Fill(tbl);
                        return tbl;
                    }
                }
            }
        }
        protected void Themhoidong()
        {
            string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            using (SqlConnection cnn = new SqlConnection(connectionString))
            using (SqlCommand cmd = new SqlCommand("Themhoidong", cnn))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@TenHiD", txtTenHD.Text);
                cmd.Parameters.AddWithValue("@Soquyetdinh", txtquyetdinh.Text);
                cmd.Parameters.AddWithValue("@Mota", txtmota.Text);
                cmd.Parameters.AddWithValue("@Trangthai", ddlTrangthai.SelectedValue);
                cnn.Open();
                cmd.ExecuteNonQuery();
                cnn.Close();
                hienhoidong();
            }
        }
        protected bool kiemtratenhoidong()
        {
            string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            string selectHD = "Select TenHiD from HOIDONG where TenHiD like N'%" + txtTenHD.Text + "%'";
            using (SqlConnection cnn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = new SqlCommand(selectHD, cnn))
                {
                    cmd.CommandType = CommandType.Text;

                    cnn.Open();
                    using (SqlDataReader rd = cmd.ExecuteReader())
                    {
                        if (rd.Read())
                        {
                            Response.Write("<script>alert('Đã tồn tại tên hội đồng')</script>");
                            return true;
                        }
                        else
                        {
                            Response.Write("<script>alert('Thêm thành công!')</script>");
                            return false;
                        }
                        rd.Close();
                    }
                    cnn.Close();
                }
            }
        }
        protected void ibtThem_Click(object sender, EventArgs e)
        {

            if (kiemtratenhoidong() == false)
            {
                Themhoidong();
            }
        }

        protected void grvhoidong_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.ToLower().Equals("xoa"))
            {
                int hoidongID = Convert.ToInt32(e.CommandArgument);
                string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
                using (SqlConnection Cnn = new SqlConnection(connectionString))
                {
                    using (SqlCommand Cmd = new SqlCommand("Xoahoidong", Cnn))
                    {
                        Cmd.CommandType = CommandType.StoredProcedure;
                        Cmd.Parameters.AddWithValue("@MaHiD", hoidongID);
                        Cnn.Open();
                        Cmd.ExecuteNonQuery();
                        Cnn.Close();
                        hienhoidong();
                    }
                }
            }
        }
        protected void delete_hoidong(int hdongID)
        {
            // if (e.CommandName.ToLower().Equals("xoa"))
            //{
            // int chuyenmucID = Convert.ToInt32(e.CommandArgument);
            string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            using (SqlConnection Cnn = new SqlConnection(connectionString))
            {
                using (SqlCommand Cmd = new SqlCommand("Xoahoidong", Cnn))
                {
                    Cmd.CommandType = CommandType.StoredProcedure;
                    Cmd.Parameters.AddWithValue("@MaHiD", hdongID);
                    Cnn.Open();
                    Cmd.ExecuteNonQuery();
                    Cnn.Close();

                }
            }
            // }
        }
        protected void ibtXoa_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow r in grvhoidong.Rows)
            {
                CheckBox cb = r.FindControl("chkchon") as CheckBox;
                if (cb != null && cb.Checked)
                {
                    int hdongID = Convert.ToInt32(grvhoidong.DataKeys[r.RowIndex].Value);
                    delete_hoidong(hdongID);
                }
            }
            hienhoidong();
        }
        protected void grvhoidong_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grvhoidong.PageIndex = e.NewPageIndex;
            hienhoidong();
            // btnXem_Click(sender, e);
        }
        protected void ibtHuy_Click(object sender, EventArgs e)
        {
            txtTenHD.Text =
            txtquyetdinh.Text =
            txtmota.Text =
         string.Empty;
            txtTenHD.Focus();
        }

        private DataTable hienthongtintimkiem()
        {

            string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            using (SqlConnection Cnn = new SqlConnection(connectionString))
            {
                using (SqlCommand Cmd = new SqlCommand("Timhoidong", Cnn))
                {
                    Cmd.CommandType = CommandType.StoredProcedure;
                    Cmd.Parameters.AddWithValue("@TenHiD", '%' + txtTimkiem.Text + '%');
                    using (SqlDataAdapter da = new SqlDataAdapter(Cmd))
                    {
                        da.SelectCommand = Cmd;
                        DataTable tb = new DataTable("HOIDONG");
                        da.Fill(tb);
                        return tb;
                    }
                }
            }
        }
        protected void ibtTimkiem_Click(object sender, EventArgs e)
        {
            using (DataTable HOIDONG1 = hienthongtintimkiem())
            {
                grvhoidong.DataSource = HOIDONG1;
                grvhoidong.DataBind();

            }
        }

    }
}